package com.db.service;

import com.db.dao.OdreceiptMapper;
import com.db.pojo.Odreceipt;
import com.db.pojo.StoreOdreceipt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("OdreceiptServiceImpl")
public class OdreceiptServiceImpl implements OdreceiptService {

    @Autowired
    private OdreceiptMapper odreceiptMapper;

    @Override
    public int insertOdreceipt(Odreceipt odreceipt) {

        return odreceiptMapper.insertOdreceipt(odreceipt);
    }

    @Override
    public int insertStoreOdreceipt(StoreOdreceipt storeodreceipt) {

        return odreceiptMapper.insertStoreOdreceipt(storeodreceipt);
    }

    @Override
    public List listOdreceipt(String startDate, String endDate, String name, String area) {
        List list = odreceiptMapper.listOdreceipt(startDate, endDate, name, area);
        return list;
    }

    @Override
    public List listStoreOdreceipt(String startDate, String endDate, String name, String area) {
        List listStoreOdreceipt = odreceiptMapper.listStoreOdreceipt(startDate, endDate, name, area);
        Map map;
        List listOdreceipt;
        if(listStoreOdreceipt.size()>0){
            for (int i = 0; i < listStoreOdreceipt.size(); i++) {
                map = (Map) listStoreOdreceipt.get(i);
                listOdreceipt = odreceiptMapper.listOdreceipt(new SimpleDateFormat("yyyy-MM-dd").format(map.get("smdate")), new SimpleDateFormat("yyyy-MM-dd").format(map.get("smdate")), (String) map.get("name"), (String) map.get("area"));
                if(listOdreceipt.size()>0){
                    map.put("state",1);
                }
                else{
                    map.put("state",0);
                }
                listStoreOdreceipt.set(i,map);
            }
        }
        return listStoreOdreceipt;
    }

    @Override
    public int updateOdreceipt(String id, String weight) {
        return odreceiptMapper.updateOdreceipt(id,weight);
    }

    @Override
    public List listOdunit() {
        List list = odreceiptMapper.listOdunit();
        return list;
    }

    @Override
    public List listOdarea() {
        List list = odreceiptMapper.listOdarea();
        return list;
    }

    @Override
    public Map selectOdunit(String name) {
        Map map = odreceiptMapper.selectOdunit(name);
        return map;
    }

}
